/*
 * Copyright (C) 2008,2009,2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

#include "exl.h"

#define r0			thread_self->r0
#define ss			thread_self->ess
#define bp			thread_self->ebp
#define sp			thread_self->esp
#define obj			thread_self->obj
#define ret			thread_self->ret
#define flt			thread_self->flt

/*
 * Implementation
 */
void
evm_dd_ne(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_cdd:
	    r0.v.i = v->v.cdd != r0.v.cdd;
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.i = mpq_get_d(cqq_realref(v->v.cqq)) != real(r0.v.cdd) ||
		     mpq_get_d(cqq_imagref(v->v.cqq)) != imag(r0.v.cdd);
	    break;
	case t_mpc:		case t_shc:
	    mpfr_set_d(thr_rr0, real(r0.v.cdd), thr_rnd);
	    if (mpfr_equal_p(mpc_realref(v->v.mpc), thr_rr0)) {
		mpfr_set_d(thr_rr0, imag(r0.v.cdd), thr_rnd);
		r0.v.i = !mpfr_equal_p(mpc_imagref(v->v.mpc), thr_rr0);
	    }
	    else
		r0.v.i = 1;
	    break;
	default:
	    r0.v.i = 1;
	    break;
    }
}

void
evm_dd_eq(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_cdd:
	    r0.v.i = v->v.cdd == r0.v.cdd;
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.i = mpq_get_d(cqq_realref(v->v.cqq)) == real(r0.v.cdd) &&
		     mpq_get_d(cqq_imagref(v->v.cqq)) == imag(r0.v.cdd);
	    break;
	case t_mpc:		case t_shc:
	    mpfr_set_d(thr_rr0, real(r0.v.cdd), thr_rnd);
	    if (mpfr_equal_p(mpc_realref(v->v.mpc), thr_rr0)) {
		mpfr_set_d(thr_rr0, imag(r0.v.cdd), thr_rnd);
		r0.v.i = mpfr_equal_p(mpc_imagref(v->v.mpc), thr_rr0);
	    }
	    else
		r0.v.i = 0;
	    break;
	default:
	    r0.v.i = 0;
	    break;
    }
}

void
evm_dd_add(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.cdd = 0.0 + r0.v.cdd;
	    chk_rdd();
	    break;
	case t_int:
	    r0.v.cdd = v->v.i + r0.v.cdd;
	    chk_rdd();
	    break;
	case t_float:
	    r0.v.cdd = v->v.d + r0.v.cdd;
	    chk_rdd();
	    break;
	case t_mpz:		case t_shz:
	    r0.v.cdd = mpz_get_d(v->v.mpz) + r0.v.cdd;
	    chk_rdd();
	    break;
	case t_mpq:		case t_shq:
	    r0.v.cdd = mpq_get_d(v->v.mpq) + r0.v.cdd;
	    chk_rdd();
	    break;
	case t_mpr:		case t_shr:
	    ecc_add_r_dd(thr_c, v->v.mpr, r0.v.cdd);
	    set_chk_rrr();
	    break;
	case t_cdd:
	    r0.v.cdd += v->v.cdd;
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.cdd = edd_add_qq_dd(v->v.cqq, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_add_cc_dd(thr_c, v->v.mpc, r0.v.cdd);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_add_cdd(v->v.o, r0.v.cdd, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_dd_sub(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.cdd = 0.0 - r0.v.cdd;
	    chk_rdd();
	    break;
	case t_int:
	    r0.v.cdd = v->v.i - r0.v.cdd;
	    chk_rdd();
	    break;
	case t_float:
	    r0.v.cdd = v->v.d - r0.v.cdd;
	    chk_rdd();
	    break;
	case t_mpz:		case t_shz:
	    r0.v.cdd = mpz_get_d(v->v.mpz) - r0.v.cdd;
	    chk_rdd();
	    break;
	case t_mpq:		case t_shq:
	    r0.v.cdd = mpq_get_d(v->v.mpq) - r0.v.cdd;
	    chk_rdd();
	    break;
	case t_mpr:		case t_shr:
	    ecc_sub_r_dd(thr_c, v->v.mpr, r0.v.cdd);
	    set_chk_rrr();
	    break;
	case t_cdd:
	    r0.v.cdd = v->v.cdd - r0.v.cdd;
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.cdd = edd_sub_qq_dd(v->v.cqq, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_sub_cc_dd(thr_c, v->v.mpc, r0.v.cdd);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_sub_cdd(v->v.o, r0.v.cdd, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_dd_mul(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.cdd = 0.0 * r0.v.cdd;
	    chk_rdd();
	    break;
	case t_int:
	    r0.v.cdd = v->v.i * r0.v.cdd;
	    chk_rdd();
	    break;
	case t_float:
	    r0.v.cdd = v->v.d * r0.v.cdd;
	    chk_rdd();
	    break;
	case t_mpz:		case t_shz:
	    r0.v.cdd = mpz_get_d(v->v.mpz) * r0.v.cdd;
	    chk_rdd();
	    break;
	case t_mpq:		case t_shq:
	    r0.v.cdd = mpq_get_d(v->v.mpq) * r0.v.cdd;
	    chk_rdd();
	    break;
	case t_mpr:		case t_shr:
	    ecc_mul_r_dd(thr_c, v->v.mpr, r0.v.cdd);
	    set_chk_rrr();
	    break;
	case t_cdd:
	    r0.v.cdd = v->v.cdd * r0.v.cdd;
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.cdd = edd_mul_qq_dd(v->v.cqq, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_mul_cc_dd(thr_c, v->v.mpc, r0.v.cdd);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_mul_cdd(v->v.o, r0.v.cdd, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_dd_div(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.cdd = 0.0 / r0.v.cdd;
	    chk_rdd();
	    break;
	case t_int:
	    r0.v.cdd = v->v.i / r0.v.cdd;
	    chk_rdd();
	    break;
	case t_float:
	    r0.v.cdd = v->v.d / r0.v.cdd;
	    chk_rdd();
	    break;
	case t_mpz:		case t_shz:
	    r0.v.cdd = mpz_get_d(v->v.mpz) / r0.v.cdd;
	    chk_rdd();
	    break;
	case t_mpq:		case t_shq:
	    r0.v.cdd = mpq_get_d(v->v.mpq) / r0.v.cdd;
	    chk_rdd();
	    break;
	case t_mpr:		case t_shr:
	    ecc_div_r_dd(thr_c, v->v.mpr, r0.v.cdd);
	    set_chk_rrr();
	    break;
	case t_cdd:
	    r0.v.cdd = v->v.cdd / r0.v.cdd;
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.cdd = edd_div_qq_dd(v->v.cqq, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_div_cc_dd(thr_c, v->v.mpc, r0.v.cdd);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_div_cdd(v->v.o, r0.v.cdd, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_dd_atan2(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.cdd = edd_atan2_i_dd(0, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_int:
	    r0.v.cdd = edd_atan2_i_dd(v->v.i, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_float:
	    r0.v.cdd = edd_atan2_d_dd(v->v.d, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_mpz:		case t_shz:
	    r0.v.cdd = edd_atan2_z_dd(v->v.mpz, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_mpq:		case t_shq:
	    r0.v.cdd = edd_atan2_q_dd(v->v.mpq, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_mpr:		case t_shr:
	    ecc_atan2_r_dd(thr_c, v->v.mpr, r0.v.cdd);
	    chk_rrr();
	    break;
	case t_cdd:
	    r0.v.cdd = edd_atan2_dd_dd(v->v.cdd, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.cdd = edd_atan2_qq_dd(v->v.cqq, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_atan2_cc_dd(thr_c, v->v.mpc, r0.v.cdd);
	    chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_atan2_cdd(v->v.o, r0.v.cdd, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_dd_pow(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.v.cdd = edd_pow_i_dd(0, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_int:
	    r0.v.cdd = edd_pow_i_dd(v->v.i, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_float:
	    r0.v.cdd = edd_pow_d_dd(v->v.d, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_mpz:		case t_shz:
	    r0.v.cdd = edd_pow_z_dd(v->v.mpz, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_mpq:		case t_shq:
	    r0.v.cdd = edd_pow_q_dd(v->v.mpq, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_mpr:		case t_shr:
	    ecc_pow_r_dd(thr_c, v->v.mpr, r0.v.cdd);
	    chk_rrr();
	    break;
	case t_cdd:
	    r0.v.cdd = edd_pow_dd_dd(v->v.cdd, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.cdd = edd_pow_qq_dd(v->v.cqq, r0.v.cdd);
	    chk_rdd();
	    break;
	case t_mpc:		case t_shc:
	    ecc_pow_cc_dd(thr_c, v->v.mpc, r0.v.cdd);
	    chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_pow_cdd(v->v.o, r0.v.cdd, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_dd_hypot(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_float;
	    r0.v.d = ed_hypot_i_dd(0, r0.v.cdd);
	    break;
	case t_int:
	    r0.t = t_float;
	    r0.v.d = ed_hypot_i_dd(v->v.i, r0.v.cdd);
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = ed_hypot_d_dd(v->v.d, r0.v.cdd);
	    break;
	case t_mpz:		case t_shz:
	    r0.t = t_float;
	    r0.v.d = ed_hypot_z_dd(v->v.mpz, r0.v.cdd);
	    break;
	case t_mpq:		case t_shq:
	    r0.t = t_float;
	    r0.v.d = ed_hypot_q_dd(v->v.mpq, r0.v.cdd);
	    break;
	case t_mpr:		case t_shr:
	    er_hypot_r_dd(thr_rr, v->v.mpr, r0.v.cdd);
	    set_rr();
	    break;
	case t_cdd:
	    /* sqrt(a*conj(a) + b*conj(b)) */
	    r0.t = t_float;
	    r0.v.d = ed_hypot_dd_dd(v->v.cdd, r0.v.cdd);
	    break;
	case t_cqq:		case t_sqq:
	    r0.t = t_float;
	    r0.v.d = ed_hypot_qq_dd(v->v.cqq, r0.v.cdd);
	    break;
	case t_mpc:		case t_shc:
	    er_hypot_cc_dd(thr_rr, v->v.mpc, r0.v.cdd);
	    set_rr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_hypot_cdd(v->v.o, r0.v.cdd, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}
